﻿using CandidateManager.Areas.Coach.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WI.CandidateManager.Business.Objects;
using WI.CandidateManager.Common.Data.Entities;

namespace CandidateManager.Areas.Coach.Controllers
{
    /// <summary>
    /// Controller for working with courseworks.
    /// </summary>
    public class CourseworksController : CandidateManager.Controllers.AutorizeBaseController
    {
        #region Coursework actions

        /// <summary>
        /// Display table with list of own Courseworks.
        /// </summary>
        /// <returns>Table with list of own Courseworks</returns>
        [Authorize]
        public ActionResult List()
        {
            CourseworkBO courseworkBO = new CourseworkBO();

            return View(courseworkBO.GetCourseworkList());
        }

        /// <summary>
        /// Display table with list of All Courseworks.
        /// </summary>
        /// <returns>Table with list of All Courseworks</returns>
        [Authorize]
        public ActionResult AllList()
        {
            CourseworkBO courseworkBO = new CourseworkBO();

            return View(courseworkBO.GetAllCourseworkList());
        }

        /// <summary>
        /// Display table with list of pending Courseworks.
        /// </summary>
        /// <returns>Table with list of pending Courseworks</returns>
        [Authorize]
        public ActionResult PendingList()
        {
            CourseworkBO courseworkBO = new CourseworkBO();

            return View(courseworkBO.GetPendingCourseworkList());
        }

        /// <summary>
        /// Get object of Coursework for displaying details.
        /// </summary>
        /// <param name="CourseworkId">Coursework id.</param>
        /// <returns>View of Coursework.</returns>
        [Authorize]
        public ActionResult Detail(long CourseworkId)
        {
            CourseworkBO courseworkBO = new CourseworkBO();

            return View(courseworkBO.GetCoursework(CourseworkId));
        }

        /// <summary>
        /// Get object of Coursework for checking.
        /// </summary>
        /// <param name="CourseworkId">Coursework id.</param>
        /// <returns>View of Coursework.</returns>
        [Authorize]
        public ActionResult Check(long CourseworkId)
        {
            return View();
        }

        /// <summary>
        /// Display page for creating new Coursework.
        /// </summary>
        /// <returns>Page for creating new Coursework.</returns>
        [HttpGet]
        [Authorize]
        public ActionResult Create()
        {
            Coursework coursework = new Coursework();

            return View(coursework);
        }

        /// <summary>
        /// Send object of Coursework to logic for creating.
        /// </summary>
        /// <param name="coursework">Object of Coursework.</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult Create(Coursework coursework)
        {
            CourseworkBO courseworkBO = new CourseworkBO();

            if (courseworkBO.CreateNewCoursework(coursework))
            {
                return RedirectToAction("List", "Courseworks");
            }
            else
            {
                return View(coursework);
            }
        }

        /// <summary>
        /// Get model of Coursework for editing.
        /// </summary>
        /// <param name="CourseworkId">Coursework id.</param>
        /// <returns>View of Coursework for editing.</returns>
        [HttpGet]
        [Authorize]
        public ActionResult Edit(long CourseworkId)
        {
            CourseworkBO courseworkBO = new CourseworkBO();
            var coursework = courseworkBO.GetCoursework(CourseworkId);

            return View(coursework);
        }

        /// <summary>
        /// Send object of Coursework to logic for editing.
        /// </summary>
        /// <param name="CourseworkId">Coursework id.</param>
        /// <param name="coursework">Object of Coursework.</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult Edit(long CourseworkId, Coursework coursework)
        {
            CourseworkBO courseworkBO = new CourseworkBO();
            var curCoursework = courseworkBO.GetCoursework(CourseworkId);

            if (courseworkBO.ChangeCoursework(CourseworkId, coursework))
            {
                return RedirectToAction("List", "Courseworks");
            }

            return View(curCoursework);
        }

        /// <summary>
        /// Get model of  Coursework for deleting.
        /// </summary>
        /// <param name="CourseworkId"> Coursework id.</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult Delete(long  CourseworkId)
        {
            CourseworkBO courseworkBO = new CourseworkBO();

            return View(courseworkBO.GetCoursework(CourseworkId));
        }

        /// <summary>
        /// Send object of Coursework to logic for deleting.
        /// </summary>
        /// <param name="CourseworkId">Coursework id.</param>
        /// <returns></returns>
        [HttpPost, ActionName("Delete")]
        [Authorize]
        public ActionResult DeleteConfirmed(long CourseworkId)
        {
            CourseworkBO courseworkBO = new CourseworkBO();

            if (courseworkBO.DeleteCoursework(CourseworkId))
            {
                return RedirectToAction("List", "Courseworks");
            }

            return View(CourseworkId);
        }

        #endregion

        #region Assign actions


        [Authorize]
        public ActionResult Assign(long CourseworkId)
        {
            CourseworkBO courseworkBO = new CourseworkBO();

            return View(courseworkBO.GetCoursework(CourseworkId));
        }


        [Authorize]
        public ActionResult RemoveCandidate(long CandidateId, long CourseworkId)
        {
            CoachBO coachBO = new CoachBO();

            coachBO.DeassingTaskToCandidate(CourseworkId, CandidateId);

            return RedirectToAction("Assign", "Courseworks", new { CourseworkId = CourseworkId });
        }


        [Authorize]
        [HttpGet]
        public ActionResult AssignToGroups(long CourseworkId)
        {
            CoachBO coachBO = new CoachBO();
            CourseworkBO courseworkBO = new CourseworkBO();

            var groups = coachBO.GetGroups();
            GroupAssignModel groupAssignModel = new GroupAssignModel();
            groupAssignModel.Task = courseworkBO.GetCoursework(CourseworkId);
            groupAssignModel.GroupCheckList = new List<GroupChecklistModel>();

            foreach (var group in groups)
            {
                groupAssignModel.GroupCheckList.Add(new GroupChecklistModel() { IsChecked = false, Group = group, GroupId = group.Id });
            }

            return View(groupAssignModel);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AssignToGroups(long TaskId, GroupAssignModel groupAssignModel)
        {
            CoachBO coachBO = new CoachBO();

            foreach (var group in groupAssignModel.GroupCheckList)
            {
                if (group.IsChecked)
                {
                    coachBO.AssignTaskToGroup(TaskId, group.GroupId);
                }
            }

            return RedirectToAction("Assign", "Courseworks", new { CourseworkId = TaskId });
        }

        [Authorize]
        [HttpGet]
        public ActionResult AssignToCandidates(long CourseworkId)
        {
            CoachBO coachBO = new CoachBO();
            CourseworkBO courseworkBO = new CourseworkBO();

            var candidates = coachBO.GetCoachesCandidates();
            CandidateAssignModel candidateAssignModel = new CandidateAssignModel();
            candidateAssignModel.Task = courseworkBO.GetCoursework(CourseworkId);
            candidateAssignModel.CandidateCheckList = new List<CandidateChecklistModel>();

            foreach (var candidate in candidates)
            {
                candidateAssignModel.CandidateCheckList.Add(new CandidateChecklistModel() { CandidateId = candidate.Id, IsChecked = false, Candidate = candidate });
            }

            return View(candidateAssignModel);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AssignToCandidates(long TaskId, CandidateAssignModel candidateAssignModel)
        {
            CoachBO coachBO = new CoachBO();

            foreach (var candidate in candidateAssignModel.CandidateCheckList)
            {
                if (candidate.IsChecked)
                {
                    coachBO.AssignTaskToCandidate(TaskId, candidate.CandidateId);
                }
            }

            return RedirectToAction("Assign", "Courseworks", new { CourseworkId = TaskId });
        }

        #endregion
    }
}
